package com.example.administrator.base.service;



import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.example.administrator.base.activity.Login;
import com.example.administrator.base.R;
import com.example.administrator.base.util.AppManager;
import com.example.administrator.base.util.SharedPreferencesUtils;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;

/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */
public class DemoIntentService extends GTIntentService {
    public DemoIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        Log.i("3378", "msg -> " + "msg = " + new String(msg.getPayload()));
        final String[] split = new String(msg.getPayload()).split(":");
        Log.i("3378945",split[0]+"woeinima "+split[1]);
        String x=split[0];
        switch (x){
            case "1":
                sendSimplestNotificationWithAction("您已退出登录",split[1],Login.class);
                SharedPreferencesUtils.setParam(this,"adminId","");
                Intent intent=new Intent(this,Login.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                AppManager.getAppManager().finishAllActivity();
                break;
            case "2":
                sendSimplestNotificationWithAction("您有一笔新的调度单!!",split[1],Login.class);
                break;
            case "3":
                sendSimplestNotificationWithAction("您有一笔新的维修单!!",split[1],Login.class);
                break;
            case "4":
                sendSimplestNotificationWithAction("您提交的订单已被接受!",split[1],Login.class);
                break;
            case "5":
                sendSimplestNotificationWithAction("您提交的订单已完成!",split[1],Login.class);
                break;
        }
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
        SharedPreferencesUtils.setParam(this,"clientid",clientid);

    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
    }
    private void sendSimplestNotificationWithAction(String title, String text,  Class<?> cls) {

        Intent mainIntent;
        NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //获取PendingIntent
        if (SharedPreferencesUtils.getParam(this,"adminId","").equals("")){
            mainIntent=new Intent(this,Login.class);
        }else {
            mainIntent = new Intent(this, cls);
        }
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        //创建 Notification.Builder 对象
        Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.car);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification)
                .setLargeIcon(bitmap)
                //点击通知后自动清除
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentText(text)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentIntent(mainPendingIntent);

        //发送通知
        mNotifyManager.notify(3, builder.build());
    }
}